<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_url_helper
 *
 * @author Simon Emms <simon@simonemms.com>
 */



function ajax_url($uri = '', $add_flags = true) {
    $controller = '/ajax/';
    return url_map($controller, $uri, $add_flags);
}


function backup_db_url($uri = '', $add_flags = true) {
    $controller = '/backup/';
    return url_map($controller, $uri, $add_flags);
}


function close_connection($uri = '', $add_flags = true) {
    $controller = '/close/';
    return url_map($controller, $uri, $add_flags);
}



function logout_url($uri = '', $add_flags = true) {
    $controller = '/logout/';
    return url_map($controller, $uri, $add_flags);
}



function new_connection_url($uri = '', $add_flags = true) {
    $controller = '/new/';
    return url_map($controller, $uri, $add_flags);
}


function restore_db_url($uri = '', $add_flags = true) {
    $controller = '/restore/';
    return url_map($controller, $uri, $add_flags);
}



function user_management_url($uri = '', $add_flags = true) {
    $controller = '/user-management/';
    return url_map($controller, $uri, $add_flags);
}


function vanilladb_login_url($uri = '', $add_flags = true) {
    $controller = '/connect/';
    return url_map($controller, $uri, $add_flags);
}


function vanilladb_logout_url($uri = '', $add_flags = true) {
    return logout_url('api');
}



/**
 * Interface URL
 * 
 * Get the URL of an interface
 * 
 * @param string $url
 * @return string
 */
function interface_url($url = '', $ui = null, $add_dir = true, $add_index = false) {
    /* Load stuff */
    $objCI = &get_instance();
    $objCI->load->library('user_interface');
    $objCI->load->helper('html');
    
    if(!is_null($ui)) { $url = $ui.'/'.$url; }
    
    /* Build the URL */
    $arrUrl = array();
    if($add_dir) {
        $arrUrl = array(
            INTERFACE_DIR,
            $objCI->user_interface->get_theme(),
        );
    }
    
    /* Get the URL */
    $url = url_map(remove_slash(implode('/', $arrUrl)), $url, false, $add_index);
    
    return $url;
}







/**
 * URL Exists
 *
 * Checks that a URL exists.  Essentially,
 * this is the file_exists() function but for
 * foreign files.
 *
 * @param string $url
 * @return bool
 */
function url_exists($url, $port = 80) {
    if(is_string($url)) {
        /* Can only check HTTP(S) sites */
        if(preg_match('/^http(s)?\:\/\//', $url)) {
            if(function_exists('fsockopen')) {
                
                /* Set the timeout to 2 second */
                $timeout = ini_get('default_socket_timeout');
                ini_set('default_socket_timeout', 2);
                
                $arrHeaders = @get_headers($url);
                
                ini_set('default_socket_timeout', $timeout);
                
                if(is_array($arrHeaders) && array_key_exists('0', $arrHeaders) && preg_match('/(200\sOK)$/i', $arrHeaders[0])) {
                    /* Valid */
                    return true;
                }
            }
        }
    }
    return false;
}



/**
 * URL Map
 *
 * Maps the URL with the given controller.  Not
 * to be called except by one of the functions in
 * this file.
 *
 * @param string $controller
 * @param string $uri
 * @return string
 * @access private
 */
function url_map($controller, $uri = '', $add_flags = true, $add_index = true) {
    /* Join controller and URI */
    $controller .= '/'.$uri;
    /* Remove the extra slashes */
    $url = remove_slash($controller);
    $CI = &get_instance();
    return $CI->config->site_url($url, $add_flags, $add_index);
}





/**
 * Site URL
 *
 * Extends the site_url to include the $add_lang
 * to the config->site_url() method
 *
 * @param string $uri
 * @param bool $add_flags
 * @return string
 */
function site_url($uri = '', $add_flags = true, $add_index = true) {
    $CI =& get_instance();
    return $CI->config->site_url($uri, $add_flags, $add_index);
}


?>